/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.router.core.domain.action;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.router.core.domain.RouterTransLog;
import info.peigen.hotpot.business.router.core.repository.entity.RouterTransLogEntity;
import info.peigen.hotpot.business.router.facade.Channel;
import info.peigen.hotpot.business.router.facade.enums.ChannelBusinessType;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>(RouterTransLogConverter)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/31
 */
public class RouterTransLogConverter {
    public static RouterTransLogEntity convert(RouterTransLog domain) {
        return BeanUtil.copyProperties(domain, RouterTransLogEntity.class)
                .setFeeCurrency(ObjectUtil.isNotNull(domain.getFeeAmount()) ? domain.getFeeAmount().getCurrency() : null)
                .setCode(domain.getChannel().getChannelCode());
    }

    public static List<RouterTransLogEntity> convert(List<RouterTransLog> domains) {
        return domains.stream().map(RouterTransLogConverter::convert).collect(Collectors.toList());
    }

    public static RouterTransLog convert(RouterTransLogEntity entity) {
        RouterTransLog transLog = BeanUtil.copyProperties(entity, RouterTransLog.class);
        transLog.getFeeAmount().setCurrency(ObjectUtil.isNotNull(entity.getFeeCurrency()) ? entity.getFeeCurrency() : entity.getPayerAccountCurrency());
        switch (entity.getAction()) {
            case Receipt:
                return transLog.setChannel(Channel.genChannel(entity.getCode(), ChannelBusinessType.Receipt));
            case Position:
                return transLog.setChannel(Channel.genChannel(entity.getCode(), ChannelBusinessType.Position));
            case Withdraw:
                return transLog.setChannel(Channel.genChannel(entity.getCode(), ChannelBusinessType.Withdraw));
            default:
                return transLog;
        }
    }

    public static List<RouterTransLog> convertBatch(List<RouterTransLogEntity> entities) {
        return entities.stream().map(RouterTransLogConverter::convert).collect(Collectors.toList());
    }
}
